package com.itgungnir.diyshuo.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.itgungnir.diyshuo.activity.R;
import com.itgungnir.diyshuo.bean.GoodsBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/10/6.
 */

public class ListAdapter extends BaseAdapter {
    private List<GoodsBean> goodsBeanList;
    private Context context;

    public ListAdapter(List<GoodsBean> goodsBeanList, Context context) {
        this.goodsBeanList = goodsBeanList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return goodsBeanList==null?0:goodsBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    ViewHolder holder = null;

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_zhinanlist, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(Uri.parse(goodsBeanList.get(position).getCoverpath())).into(holder.znCover);
        holder.znSubject.setText(goodsBeanList.get(position).getSubject());
        return convertView;
    }

    //自定义ViewHolder
    static class ViewHolder {
        ImageView znCover;
        TextView znSubject;
        ViewHolder(View view) {
            znCover = (ImageView) view.findViewById(R.id.zn_cover);
            znSubject = (TextView) view.findViewById(R.id.zn_subject);
        }
    }

    public void reloadRecyclerView(List<GoodsBean> _list, boolean isClear) {
        if (isClear) {
            goodsBeanList.clear();
        }
        goodsBeanList.addAll(_list);
        notifyDataSetChanged();
    }
}
